<?php
//URL: http://localhost/ChampChooser/core/services/team/updateToTeamService.php?teamId=4002&playerIds=5004,5010,5011,5012,5013,5014,5015,5016,5017,5018,5020
require_once ("../../config/config.php");
require_once ("../../config/constants.php");
require_once ("../../db/queries.php");
require_once ("../../common/common.php");

$teamId = $_REQUEST["teamId"];
$playerIds = $_REQUEST["playerIds"];

function updateToTeam($teamId,$playerIds) {
	$playersList = split(',',$playerIds);
	$strQuery="select * from team where id = ".$teamId;
	$result=$result = mysql_query($strQuery);
	if($result && mysql_num_rows($result)>0){
		$strQuery = "update cc_v1.`team` set Batsman1Id = '".$playersList[0]."', Batsman2Id = '".$playersList[1]."', Batsman3Id = '".$playersList[2]."', Batsman4Id = '".$playersList[3]."', Batsman5Id = '".$playersList[4]."', WicketkeeperId = '".$playersList[5]."', AllRounderId = '".$playersList[6]."', Bowler1Id = '".$playersList[7]."', Bowler2Id = '".$playersList[8]."', Bowler3Id = '".$playersList[9]."', Bowler4Id = '".$playersList[10]."' where id = '".$teamId."';";
	}else{
		$strQuery = "insert into cc_v1.`team`(Batsman1Id,Batsman2Id,Batsman3Id,Batsman4Id,Batsman5Id,WicketkeeperId,AllRounderId,Bowler1Id,Bowler2Id,Bowler3Id,Bowler4Id) VALUES ('";
		$strQuery = $strQuery.$playersList[0]."'";
		for ( $index = 1, $max_count = sizeof( $playersList ); $index < 11; $index++ ) {
			$strQuery = $strQuery." , '".$playersList[$index]."'";
		}
		$strQuery = $strQuery." );";
	}
	$result = generateResultSet($strQuery);
	if (!$result) {
		die("Something is Seriously wrong!!!!");
	} else{
		$strQuery = "select * from cc_v1.`team` where id = '".$teamId."'";
		makeTeamQueryXML(generateResultSet($strQuery));
		/*$strQuery = "select * from cc_v1.`player` where id = '".$playersList[0]."'";
		for ( $index = 1, $max_count = sizeof( $playersList ); $index < $max_count; $index++ ) {
			$strQuery = $strQuery." or id = '".$playersList[$index]."'";
		}*/
	}
	//return generateResultSet($strQuery);
}

updateToTeam($teamId,$playerIds);

?>